	var lazyLoader;

	function onLazyLoadShow( data )
	{
		placeLads( data );
	}

			<!-- // script for 'filter' dialog -->
			function setMenu( obj )
			{
				$( '.menu-item' ).each(	function( index )
										{
											$( this ).removeClass( 'active-menu-item' );
											$( this ).addClass( 'inactive-menu-item' );
										} );
				$( obj ).removeClass( 'inactive-menu-item' );
				$( obj ).addClass( 'active-menu-item' );
			}
			function onAllGirls( obj )
			{
				setMenu( obj );

				$.ajax(	{
							'url': "/all-lads/",
							'dataType': "json",
							'success': filterLadsSuccess,
							'error': filterLadsError
						}
				);

				lazyLoader.updateUrl( '/lazy-load/' );
				lazyLoader.updateData( {} );

				document.location.href = "#";
			}
			function onFilter( obj )
			{
				setMenu( obj );
			}
			function onAddGirl( obj )
			{
				setMenu( obj );
			}

			function afterShow()
			{
				var nameSurname = $( '[name=name_surname]' );
				var age = $( '[name=age]' );
				nameSurname.focus();
				nameSurname.val( "" );
				age.val( "" );
			}

			
			// returns parameter value from anchor to filter
			function getAnchorFilterParam( paramRe )
			{
				var re = new RegExp( paramRe );
				var m = document.location.href.match( re );
				return m[ 1 ];
			}

			function setupMenu()
			{
				//$( 'span.menu-item' ).
			}
			$( document ).ready(	function() 
									{ 
										$( '.fancybox' ).fancybox(	{ 
																		'afterShow': afterShow, 
																		afterClose: afterClose, 
																		helpers: 
																		{
																			overlay : 
																			{
																				locked : false
																			} 
																		} 
																	} 
										); 

										var href = document.location.href;

										
										lazyLoader = 	new LazyLoader(
											{
												'url': "/lazy-load/",
												'data': {},
												'bottomMargin': 80,
												'showCallback': onLazyLoadShow,
												'finishedCallback': function() { $( '#next-page' ).hide() }
											}
										);																								

										lazyLoader.updateUrl( '/lazy-load/' );
										lazyLoader.updateData( {} );


										var isMinAge = ( href.indexOf( 'min_age=' ) != -1 );
										var isMaxAge = ( href.indexOf( 'max_age=' ) != -1 );

										if( href.indexOf( '#filter-lads' ) != -1 )
										{
											$( '[href="#filter-lads"]' ).trigger( 'click' );
										}
										else
										if( href.indexOf( '#add-lad' ) != -1 )
										{
											$( '[href="#add-lad"]' ).trigger( 'click' );
										}
										else
										// handle search parameters
										if( href.indexOf( '#career=' ) != -1 )
										{
											filterLadsByData( { 'career': getAnchorFilterParam( "#career=(\\d+)" ) } );
										}
										else
										if( href.indexOf( '#figure_type=' ) != -1 )
										{
											filterLadsByData( { 'figure_type': getAnchorFilterParam( "#figure_type=(\\d+)" ) } );											
										}
										else
										if( isMinAge || isMaxAge )
										{
											var data = {}
											if( isMinAge )
											{
												data[ 'age__gte' ] = getAnchorFilterParam( "min_age=(\\d+)" );
											}
											
											if( isMaxAge )
											{
												data[ 'age__lte' ] = getAnchorFilterParam( "max_age=(\\d+)" );
											}

											filterLadsByData( data );
										}
																			
										lazyLoader.init();
									}								 
			);

			function collectFormData( form )
			{
        		var elements = [ 'input', 'select' ];
        		var data = {};
        		for( var iEl in elements )
        		{
        			form.find( elements[ iEl ] ).each(	function()
        												{
															var name = $( this ).attr( 'name' );
															var val = $( this ).val();
															data[ name ] = val;
														}	
					);
        		}
        		return data;
			}

        	function sendFormAjax( form, data, success, error )
        	{
        		var url = form.attr( 'action' );
        		var type = form.attr( 'method' );
				$.ajax(	{
							'url': url,
							'type': type,
							'data': data,
							'dataType': "json",
							'success': success,
							'error': error
						} );
        	}
        	function setLoaderState( isLoader, index )
        	{
        		var postfix = "";
        		if( index != null )
        		{
        			postfix = String( index );
        		}
        		if( isLoader )
        		{
        			$( '#button' + postfix ).removeClass( 'form-button-bk' );
					$( '#button' + postfix ).addClass( 'form-button-bk-loader' );
					$( '#button-caption' + postfix ).hide();
					$( '#loader' + postfix ).show();
				}
				else
				{
        			$( '#button' + postfix ).addClass( 'form-button-bk' );
					$( '#button' + postfix ).removeClass( 'form-button-bk-loader' );
					$( '#button-caption' + postfix ).show();
					$( '#loader' + postfix ).hide();
				}
        	}

        	function smoothScrollTop()
        	{
        		$( 'html, body' ).animate( { scrollTop: 0 }, 1000 );
        	}

        	function placeLads( data )
        	{
        		for( var iLad in data )
        		{
        			placeLad( data[ iLad ], false );
        		}
        	}
        	
        	function placeLad( itemData, isPrepend )
        	{
        		var container = $( '#item-container' );
				var item = $( '#item-template' ).clone();
				var img = item.find( 'img' );
				var id = String( itemData[ 'id' ] );
				img.attr( 'id', 'thumbnail-' + id );
				img.attr( 'src', itemData[ 'thumbnail' ] );
				img.attr( 'onclick', "showPhotos(" + id + ")" );
				var aNameSurname = item.find( 'a.name_surname' );
				aNameSurname.html( itemData[ 'name_surname' ] );
				aNameSurname.attr( 'href', "/" + itemData[ 'slug' ] );
				item.find( 'span.career' ).html( itemData[ 'career' ] );
				item.show();

				if( isPrepend )
				{
					container.prepend( item );
				}
				else
				{
					container.append( item );
				}
        	}

        	// the function place data parameters after anchor '#' sign
        	function filteredDataToAnchor( data )
        	{
        		var params = [];

        		for( var i in data )
        		{
        			params.push( i + "=" + data[ i ] );
        		}

        		document.location.href = '#' + params.join( '&' );
        	}

        	var regime;

        	function filterLadsByForm()
        	{
	        	var form = $( '#filter-lads-form' );
        		var data = collectFormData( form );

        		filteredDataToAnchor( data );

				filterLadsByData( data );				
        	}

        	function filterLadsByData( data )
        	{
        		regime = 'filter-lads';
        		var form = $( '#filter-lads-form' );
        		sendFormAjax( form, data, filterLadsSuccess, filterLadsError );
        		setLoaderState( true, 2 );

        		lazyLoader.updateData( data );
        		lazyLoader.updateUrl( '/filter-lads/' );        		
        	}

        	function filterLadsSuccess( data )
        	{

        		// remove 'not found'
        		$( '#not-found' ).remove();

				// check			

				var result = data[ 'result' ];

				var container = $( '#item-container' );
				container.html( "" );

				if( result.length > 0 )
				{
					// found

					for( var i in result )
					{
						var itemData = result[ i ];

						placeLad( itemData, false );
					}

					lazyLoader.reset();
				}
				else
				{
					var notFound = $( '#not-found-template' ).clone();
					notFound.attr( 'id', 'not-found' );
					notFound.show();
					container.append( notFound );

				}

				$.fancybox.close();

			  	setLoaderState( false, 2 );
			  	smoothScrollTop();
        	}

        	function filterLadsError()
        	{
        		setLoaderState( false, 2 );
        	}


        	
        	function addLad()
        	{        		
        		regime = 'add-lad';
        		var form = $( '#add-lad-form' );
        		var data = collectFormData( form );
        		data.track_id = trackId;
        		sendFormAjax( form, data, addLadSuccess, addLadError );
				setLoaderState( true );
        	}

        	var imageWidth;
        	var imageHeight;

        	// setup the variable after adding new girl
        	var cropId;

			function preview(coords)
			{
				var rx = 260 / coords.w;
				var ry = 260 / coords.h;
			
				$( '#' + cropId ).css({
					width: Math.round(rx * imageWidth) + 'px',
					height: Math.round(ry * imageHeight) + 'px',
					marginLeft: '-' + Math.round(rx * coords.x) + 'px',
					marginTop: '-' + Math.round(ry * coords.y) + 'px'
				});
			}

			var size;
			var x;
			var y;
			var ladId;		
			
			function afterClose()
			{
				if( successClose && regime == 'add-lad' )
				{

					if( successData[ 'result' ] == "FOUND" )
					{
						var container = $( '#item-container' );
						container.html( "" );
						placeLad( successData[ 'lad' ], false );
						$.fancybox(	$( '#found' ), 
									{	
										'fitToView': true, 'scrolling': "no", 
										'helpers' : 
										{
											'overlay' : 
											{
												'locked' : false
											} 
										} 
									} 
						);
					}					
					else
					{
						// suppose: successData[ 'result' ] == 'OK'
						placeLad(	{	
										'id': successData[ 'id' ],
										'thumbnail': successData[ 'url' ],
										'name_surname': successData[ 'profile' ][ 'name_surname' ],
										'career': successData[ 'profile' ][ 'career' ],
										'slug': successData[ 'slug' ]
									}, true );	

						smoothScrollTop();
					}
				}
			}

			var successClose = false;
			var successData;

			function handleErrorField( errors, name )
			{
				if( errors[ name ] != null )
				{
					var selector = '[name=' + name + ']';
					var field = $( selector );
					field.css( 'border-color', "red" );

					$( selector ).focus( function() { field.css( 'border-color', "black" ); field.off( 'focus' ); } );
				}
			}

        	function addLadSuccess( data )
        	{
        		if( data[ 'result' ] == "ERROR" )
        		{
					successClose = false;
					var errors = data[ 'errors' ];

					handleErrorField( errors, 'age' );
					handleErrorField( errors, 'name_surname' );
        		}
        		else
        		{
	        		successClose = true;				
					$.fancybox.close();
					successData = data;
        		}

        		setLoaderState( false );

        		smoothScrollTop();
        	}
        	function addLadError()
        	{
        		successClose = false;
				setLoaderState( false );
        	}

        	function showPhotos( ladId )
        	{
        		$.fancybox.showLoading();
        		$.ajax(	
        			{
        				'url': "/get-photos/",
        				'data': { 'id': ladId },
        				'dataType': "json",
						'success': showPhotosSuccess,
						'error': showPhotosError
        			}
				);
        				
        	}

        	function showPhotosSuccess( data )
        	{
				$.fancybox.hideLoading();
				$.fancybox( data, { 'helpers' : 
															{
																'overlay' : 
																{
																	'locked' : false
																} 
															} }  );
        	}

        	function showPhotosError()
        	{
				$.fancybox.hideLoading();
        	}